package com.itchen.repository.impl.mapper;

import com.itchen.emtity.po.UploadPO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FileUploadMapper {

    /**
     * 添加上传文件后的数据
     * @param uploadData
     * @return
     */
    @Insert("insert into upload " +
                "(upload_id,file_size,file_type,original_name,create_time,file_path,new_name,download_number) " +
            "values " +
                "(#{uploadId},#{fileSize},#{fileType},#{originalName},#{createTime},#{filePath},#{newName},#{downloadNumber})")
    int createUploadData(UploadPO uploadData);

    /**
     * 查询上传文件数据
     * @return
     */
    @Select({"<script>" +
            "select " +
                "upload_id,file_size,file_type,original_name,create_time,file_path,is_deleted,new_name,download_number " +
            "from " +
                "upload " +
            "where " +
                "is_deleted = false" +
            "<when test='orName != null'>" +
            "and original_name like concat('%',#{orName},'%') "+
            "</when>"+
            "</script>"})
    List<UploadPO> getUploadFileList(String orName);

    /**
     * 单个删除数据
     * @param uploadId
     * @return
     */
    @Delete("update " +
                "upload " +
            "set " +
                "is_deleted = true " +
            "where " +
                "upload_id = #{uploadId} and is_deleted = false")
    int deleteUploadFileDataById(String uploadId);

    /**
     * 查找总数
     * @return
     */
    @Select("select " +
                "count(upload_id) " +
            "from " +
                "upload " +
            "where " +
                "is_deleted = false")
    int getCount();

    @Select("select " +
                "upload_id,file_size,file_type,original_name,create_time,file_path,is_deleted,new_name,download_number " +
            "from " +
                "upload " +
            "where " +
                "upload_id = #{uploadId} and is_deleted = false")
    UploadPO getUploadFileById(String uploadId);


    /**
     * 下载数量加一
     * @param uploadId
     * @param downloadNumber
     */
    @Update("update " +
                "upload " +
            "set " +
                "download_number = #{downloadNumber} " +
            "where " +
                "upload_id = #{uploadId} and is_deleted = false")
    void downloadNumberAddOne(@Param("uploadId") String uploadId, @Param("downloadNumber") int downloadNumber);

    /**
     * 批量删除
     * @param uploadPOS
     * @return
     */
    @Update({"<script>" +
            "update " +
                "upload " +
            "set " +
                "is_deleted = case upload_id " +
            "<foreach collection='ups' item='up'>" +
            "   when #{up.uploadId} then true " +
            "</foreach> " +
            "end " +
            "where " +
                "upload_id in " +
            "<foreach collection='ups' item='up' open='(' close=')' separator=','>"+
            "  #{up.uploadId} " +
            "</foreach>" +
            "and is_deleted = false" +
            "</script>"})
    int deleteUploadFileDataByIds(@Param("ups") List<UploadPO> uploadPOS);
}
